<?php
/**
 * Main View class
 */
class Main_View extends Psa_Smarty_View{
	
	
	/**
	 * Login page.
	 */
	function login($msg_type = null){
		
		// authorization error
		if($msg_type == 'unsuccessful'){
			$this->psa_smarty->assign('error', 'You entered invalid username or password.');
		}
		
		$this->psa_smarty->assign('header_main', $this->psa_smarty->fetch('header_main.tpl'));
		$this->psa_smarty->assign('content_main', $this->psa_smarty->fetch("login.tpl"));
	}
	
	
	/**
	 * Main layout render and display method.
	 * This method determine if request is made by ajax or not and echo only
	 * ajax result or whole html page.
	 */
	function generate_html(){
		
		
		// if this is ajax request just echo content
		if(@$this->psa_result->ajax_request){
			header ("Content-Type: text/html; charset=utf-8");
			if(isset($this->psa_result->ajax_response))
				echo $this->psa_result->ajax_response;
			else
				echo @$this->psa_smarty->getTemplateVars('content');
		}
		// render complete page
		else{
			
			if(isset($this->psa_registry->CFG['js_files']))
				$this->psa_smarty->assign('include_js', $this->psa_registry->CFG['js_files']);
			if(isset($this->psa_registry->CFG['css_files']))
				$this->psa_smarty->assign('include_css', $this->psa_registry->CFG['css_files']);
			
			// put user into template values
			if(isset($this->psa_registry->user->username))
				$this->psa_smarty->assign('username', $this->psa_registry->user->username);
			if(isset($this->psa_registry->user->full_name))
				$this->psa_smarty->assign('full_name', $this->psa_registry->user->full_name);
			
			// asign page header and main menu
			$this->psa_smarty->assign('header_fixed', $this->psa_registry->CFG['header']['fixed']);
			$this->psa_smarty->assign('header_main', $this->psa_smarty->fetch('header_main.tpl'));
			$this->psa_smarty->assign('layout_left', $this->psa_smarty->fetch('menu_main.tpl'));
			$this->psa_smarty->assign('ace_settings_allowed', $this->psa_registry->CFG['ace_settings']['allowed']);
			$this->psa_smarty->assign('ace_settings', $this->psa_smarty->fetch('ace_settings.tpl'));
			
			// html page main content
			if(!$this->psa_smarty->getTemplateVars('content_main')){
				$this->psa_smarty->assign('content_main', $this->psa_smarty->fetch('layout_main.tpl'));
			}
			
			// echo complete rendered page
			echo $this->psa_smarty->fetch('main.tpl');
		}
	}
	
	
	function redirect($url = null){
		
		$url = $this->psa_registry->basedir_web . '/' . $url;
		
		header("Location: " . $url);
		exit;
	}
	
	
	/**
	 * On uncaught exception
	 * @param exception $e
	 */
	function uncaught_exception($e){
		
		// get data from exception
		$exception['code'] = $e->getCode();
		$exception['msg'] = $e->getMessage();
		$exception['type'] = get_class($e);
		
		// if this is ajax request
		if(isset($this->psa_result->ajax_request) && $this->psa_result->ajax_request){
			
			// send 500 header
			if(!headers_sent())
				header('HTTP/1.1 500 Internal Server Error');
			
			$this->psa_smarty->assign("exception", $exception);
			echo $this->psa_smarty->fetch("exception_ajax.tpl");
		}
		else{
			$this->psa_smarty->assign("exception", $exception);
			$this->psa_smarty->assign('content', $this->psa_smarty->fetch('exception.tpl'));
			$this->generate_html();
		}
		
		exit;
	}	
}
